from django.shortcuts import render
from .models import *
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .ser import *
# Create your views here.

# A4作业
# class Moviemanage(APIView):
#     def post(self,request):
#         ser = Movieser(data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'code':200})
#         else:
#             print(ser.errors)
#             return Response({'code':20010})
#     def get(self,request):
#         se = Movie.objects.all()
#         ser = Movieser(se,many=True)
#         return Response({'code':200,'list':ser.data})
#
# class Actormanage(APIView):
#     def post(self,request):
#         ser = Actorser(data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'code':200})
#         else:
#             print(ser.errors)
#             return Response({'code':20010})
#     def get(self,request):
#         se = Actor.objects.all()
#         ser = Actorser(se,many=True)
#         return Response({'code':200,'list':ser.data})
#
#
# # A1作业
# class Teachermanage(APIView):
#     def post(self,request):
#         ser = Teacherser(data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'code': 200})
#         else:
#             print(ser.errors)
#             return Response({'code': 20010})
#     def get(self,request):
#         username = request.data.get('username')
#         se = Teacher.objects.filter(username=username).all()
#         ser = Teacherser(se,many=True)
#         return Response({'code':200,'list':ser.data})
#
# class TeacherM(APIView):
#     def post(self,request):
#         username = request.data.get('username')
#         password = request.data.get('password')
#         use = Teacher.objects.filter(username=username).all()
#         if username==use.username:
#             if password == use.password:
#                 return Response({'code':200,'userid':use.id})
#             else:
#                 return Response({'code':20010,'meg':'密码错误'})
#         else:
#             return Response({'code':20011,'meg':'用户名不存在'})
#
# class Studentmanage(APIView):
#     def post(self,request):
#         ser = Studentser(data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'code': 200})
#         else:
#             print(ser.errors)
#             return Response({'code': 20010})
#

class Stureg(APIView):
    def post(self,request):
        ser = Stuser(data=request.data)
        if ser.is_valid():
            ser.save()
            stu = Stu.objects.filter(username=request.data.get('username')).first()
            return Response({'code':200,'id':stu.id,'name':stu.name})
        else:
            print(ser.errors)
            return Response({'code':20010})

    def get(self,request):
        id = request.GET.get('id')
        stu = Stu.objects.filter(id=id).first()
        ser = ShowStuser(stu)
        return Response({'code':200,'list':ser.data})

# 课程
class ShowCourse(APIView):
    def get(self,request):
        cla = Sclass.objects.all()
        ser = Sclassser(cla,many=True)
        return Response({'code':200,'list':ser.data})